package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.PluSalesRpt;

public class PluSalesRptService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;

	public PluSalesRptService() {
	}

	public PluSalesRptService(Context context) {
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}

	public void DeleteAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM " + DatabaseSchema.PluSalesRpt.SCHEMA_NAME);
	}

	public void DeleteByID(long id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM " + DatabaseSchema.PluSalesRpt.SCHEMA_NAME
				+ " WHERE " + DatabaseSchema.PluSalesRpt.US_ID + " = ?",
				new Object[] { id });
	}

	public List<PluSalesRpt> FindAll() {
		List<PluSalesRpt> obj = new ArrayList<PluSalesRpt>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery("select * from "
				+ DatabaseSchema.PluSalesRpt.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int us_id = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_ID));
			int us_store_id = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_STORE_ID));
			int us_station = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_STATION));
			String us_date = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_DATE));
			String us_upc_plu = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_UPC_PLU));
			int us_qty = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_QTY));
			double us_weight = cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_WEIGHT));
			String us_wt_unit = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_WT_UNIT));
			double us_amt = cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_AMT));
			String us_type = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_TYPE));
			int us_sub_dep = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.PluSalesRpt.US_SUB_DEP));

			obj.add(new PluSalesRpt(us_id, us_store_id, us_station, us_date,
					us_upc_plu, us_qty, us_weight, us_wt_unit, us_amt, us_type,
					us_sub_dep));
		}
		cursor.close();
		return obj;
	}

	public void InsertOrUpdatePluSalesRpt(PluSalesRpt obj) {
		if (CheckPluInList(obj.getUs_upc_plu(), obj.getus_date(),
				obj.getUs_type(), obj.getUs_sub_dep())) {
			this.Update(obj);
		} else {
			this.Insert(obj);
		}
	}

	public boolean CheckPluInList(String upc, String date, String type,
			int sub_dep) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery("select us_upc_plu from "
				+ DatabaseSchema.PluSalesRpt.SCHEMA_NAME
				+ " where us_upc_plu = '" + upc + "' and us_date = '" + date
				+ "' and us_type = '" + type + "'" + " and us_sub_dep = "
				+ sub_dep, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return false;
		}
		if (cursor.moveToNext()) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}

	public void Insert(PluSalesRpt obj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO "
						+ DatabaseSchema.PluSalesRpt.SCHEMA_NAME
						+ " VALUES(?,?,?,?,?,?,?,?,?,"
						+ "strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'),?,?)",
				new Object[] { null, obj.getus_store_id(), obj.getus_station(),
						obj.getus_date(), obj.getUs_upc_plu(), obj.getus_qty(),
						obj.getus_weight(), obj.getus_wt_unit(),
						obj.getus_amt(), obj.getUs_type(), obj.getUs_sub_dep() });
	}

	public void Update(PluSalesRpt obj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Update "
						+ DatabaseSchema.PluSalesRpt.SCHEMA_NAME
						+ " set us_date = ?,us_qty = us_qty + ?,"
						+ "us_weight = us_weight + ?,us_wt_unit = ?,us_amt = us_amt + ?,update_datetime = strftime('%Y%m%d%H%M%S','now'),"
						+ "us_type = ?,us_sub_dep = ? where us_upc_plu = ? and us_date = ? and us_type = ? and us_sub_dep = ?",
				new Object[] { obj.getus_date(), obj.getus_qty(),
						obj.getus_weight(), obj.getus_wt_unit(),
						obj.getus_amt(), obj.getUs_type(), obj.getUs_sub_dep(),
						obj.getUs_upc_plu(), obj.getus_date() ,obj.getUs_type() ,obj.getUs_sub_dep()});
	}

	public List<PluSalesRpt> getPLUSalesRptByDate(String Date) {
		List<PluSalesRpt> list = new ArrayList<PluSalesRpt>();
		String sql = "select us_id,us_store_id,us_station,us_date,us_upc_plu,sum(us_qty),us_weight,us_wt_unit,sum(us_amt),create_datetime,update_datetime,us_type,us_sub_dep from t_plu_sales_rpt where us_date = '"
				+ Date
				+ "' group by us_store_id,us_station,us_date,us_upc_plu,us_type order by us_id,us_upc_plu";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null)
			return list;
		while (cursor.moveToNext()) {
			PluSalesRpt item = new PluSalesRpt();
			item.setus_id(cursor.getInt(0));
			item.setus_store_id(cursor.getInt(1));
			item.setus_station(cursor.getInt(2));
			item.setus_date(cursor.getString(3));
			item.setUs_upc_plu(cursor.getString(4));
			item.setus_qty(cursor.getInt(5));
			item.setus_weight(cursor.getDouble(6));
			item.setus_wt_unit(cursor.getString(7));
			item.setus_amt(cursor.getDouble(8));
			item.setUs_type(cursor.getString(11));
			item.setUs_sub_dep(cursor.getInt(12));
			list.add(item);
		}
		cursor.close();
		return list;
	}

	public String getItemName(String PLU) {
		String sql = "select F02 from t_item_setting where F01 = '" + PLU + "'";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null)
			return "";
		if (cursor.moveToNext()) {
			String name = cursor.getString(0);
			cursor.close();
			return name;
		}
		return "";
	}

	public String getItemDiscount(String PLU) {
		String sql = "select ds_desc from t_discount_setting where ds_no = "
				+ PLU;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null)
			return "";
		if (cursor.moveToNext()) {
			String name = cursor.getString(0);
			cursor.close();
			return name;
		}
		return "";
	}

	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
